package cn.ihuizi.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.xml.internal.bind.v2.runtime.Name;

import cn.ihuizi.dao.PowerDao;
import cn.ihuizi.dao.impl.PowerDaoImpl;
import cn.ihuizi.entity.Admin;
import cn.ihuizi.service.LoginService;
import cn.ihuizi.service.PowerService;
import cn.ihuizi.service.impl.LoginServiceImpl;
import cn.ihuizi.service.impl.PowerServiceImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*response.getWriter().append("Served at: ").append(request.getContextPath());*/
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String account = request.getParameter("account");
		String pwd = request.getParameter("pwd");
		account=new String(account.getBytes("ISO-8859-1"),"utf-8");
		LoginService loginService = new LoginServiceImpl();
		boolean b = loginService.login(new Admin(account,pwd));
		HttpSession session = request.getSession();
		session.setAttribute("account", account);
		session.setAttribute("pwd", pwd);
		System.out.println(account);
		System.out.println(pwd);
		//获取权限
		PowerService powerService = new PowerServiceImpl();
		
		List<Integer> list= powerService.AdminPowerId(new Admin(account));
		System.out.println(list);
		session.setAttribute("power", list);
		
		if(b) {
		
			response.sendRedirect("view/index.jsp");
			/*response.getWriter().write("true");*/
			System.out.println("成功");
		}else {
			session.removeAttribute("error");
			session.setAttribute("error", "用户名密码错误,请重新输入");
			response.sendRedirect("view/login.jsp");
			System.out.println("失败");
			/*request.getRequestDispatcher("view/login.jsp").forward(request, response);*/
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
